﻿// <copyright file="DomainActionImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class DomainActionImporter : VisualCollectionImporter<DomainAction>
    {
        private readonly int Visibility = 0;
        private readonly int Domain = 1;
        private readonly int DomainTurn = 2;
        private readonly int DomainRound = 3;
        private readonly int BaseAction = 4;
        private readonly int CostGB = 5;
        private readonly int CostRP = 6;
        private readonly int ExtraGB = 7;
        private readonly int ExtraRP = 8;
        private readonly int OwnerInformation = 9;
        private readonly int GMInformation = 10;

        private IEnumerable<BaseAction> baseActions;
        private IEnumerable<DomainTurn> domainTurns;

        public DomainActionImporter(DataWorkspace workspace)
            : base(workspace)
        {
        }

        protected override void FetchRequisites()
        {
            this.baseActions = this.workspace.ApplicationData.BaseActions.GetQuery().Execute();
            this.domainTurns = this.workspace.ApplicationData.DomainTurns.GetQuery().Execute();
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[11];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Domain", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Domain] = i;
                }
                else if (stringHeaders[i].StartsWith("Turn", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[DomainTurn] = i;
                }
                else if (stringHeaders[i].StartsWith("Round", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[DomainRound] = i;
                }
                else if (stringHeaders[i].StartsWith("Action", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[BaseAction] = i;
                }
                else if (stringHeaders[i].StartsWith("CostGB", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[CostGB] = i;
                }
                else if (stringHeaders[i].StartsWith("CostRP", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[CostRP] = i;
                }
                else if (stringHeaders[i].StartsWith("ExtraGB", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[ExtraGB] = i;
                }
                else if (stringHeaders[i].StartsWith("ExtraRP", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[ExtraRP] = i;
                }
                else if (stringHeaders[i].StartsWith("Own", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[OwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }
            }

            return headers;
        }

        protected override bool SkipEntity(int index)
        {
            return false;
        }

        protected override void FillEntity(DomainAction entity, int index, int?[] headers, string[] data)
        {
            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value], out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[Domain].HasValue && headers[DomainTurn].HasValue && headers[DomainRound].HasValue)
            {
                var turn = this.domainTurns.Where(dt => dt.Domain.Name.Equals(data[index + headers[Domain].Value].Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                                                        dt.Turn.ToString().Equals(data[index + headers[DomainTurn].Value].Trim(), StringComparison.InvariantCultureIgnoreCase))
                                            .FirstOrDefault();

                if (turn != null)
                {
                    switch (data[index + headers[Domain].Value].Trim())
                    {
                        case "1":
                            turn.FirstRoundActions.Add(entity);
                            break;
                        case "2":
                            turn.SecondRoundActions.Add(entity);
                            break;
                        case "3":
                            turn.ThirdRoundActions.Add(entity);
                            break;
                    }
                }
            }

            if (headers[BaseAction].HasValue)
            {
                entity.BaseAction = this.baseActions.FirstOrDefault(ba => ba.Name.Equals(this.data[index + headers[BaseAction].Value], StringComparison.InvariantCultureIgnoreCase));
            }

            if (headers[CostGB].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[CostGB].Value], out value))
                {
                    entity.BaseCostGB = value;
                }
            }

            if (headers[CostRP].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[CostRP].Value], out value))
                {
                    entity.BaseCostRP = value;
                }
            }

            if (headers[ExtraGB].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[ExtraGB].Value], out value))
                {
                    entity.ExtraGB = value;
                }
            }

            if (headers[ExtraRP].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[ExtraRP].Value], out value))
                {
                    entity.ExtraRP = value;
                }
            }

            if (headers[OwnerInformation].HasValue)
            {
                entity.OwnerInformation = data[index + headers[OwnerInformation].Value];
            }

            if (headers[GMInformation].HasValue)
            {
                entity.GMInformation = data[index + headers[GMInformation].Value];
            }
        }
    }
}
